/***********************************************************************************************************************
 * 
 * > QuadroCore <
 * 
 * Copyright (C) 2012 by Chris Channing
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************/

#include "quadrocore.h"

void LCDCanvasDrawLine(uint8_t x, uint8_t y, uint8_t x2, uint8_t y2)
{
    uint8_t w = x2 - x;
    uint8_t h = y2 - y;
    uint8_t dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
	
    if (w < 0) dx1 = -1; else if (w > 0) dx1 = 1;
    if (h < 0) dy1 = -1; else if (h > 0) dy1 = 1;
    if (w < 0) dx2 = -1; else if (w > 0) dx2 = 1;
	
    uint8_t longest = abs(w);
    uint8_t shortest = abs(h);
	
    if (! (longest > shortest)) 
	{
	    longest = abs(h);
	    shortest = abs(w);
		
	    if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1;
		
	    dx2 = 0 ;
    }
	
	{
		uint8_t numerator = longest >> 1;
		
		for (uint8_t i = 0; i <= longest; i++) 
		{
			LCDCanvasDrawPixel(x, y);
		
			numerator += shortest;
			
			if (! (numerator < longest)) 
			{
				numerator -= longest;
				x += dx1;
				y += dy1;
			} 
			else 
			{
				x += dx2;
				y += dy2;
			}
		}
	}		
}

void LCDCanvasDrawBox(uint8_t x, uint8_t y, uint8_t height, uint8_t width, bool_t fill)
{
	if ((x < LCD_WIDTH) && (y < LCD_HEIGHT))
	{
		uint8_t blX = x;
		uint8_t blY = y;
		uint8_t brX = x + width;
		uint8_t brY = y;
				
		uint8_t tlX = x;
		uint8_t tlY = y + height;
		uint8_t trX = x + width;
		uint8_t trY = y + height;
		
		LCDCanvasDrawLine(blX, blY, brX, brY);
		LCDCanvasDrawLine(tlX, tlY, trX, trY);
		LCDCanvasDrawLine(blX, blY, tlX, tlY);
		LCDCanvasDrawLine(brX, brY, trX, trY);
		
		if (fill)
		{
			for (int yy = blY; yy < (blY + height); yy++)
			{
				LCDCanvasDrawLine(blX, yy, brX, yy);
			}
		}
	}
}